package com.sgb.kjwl.view.ui.adapter.self.publish;

import android.content.Context;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.link.entity.BrandEntity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.ArrayList;
import java.util.List;

public class BrandAdapter extends BaseRecycleAdapter<BrandEntity> {

    private TextView brand_tv;
    private RecyclerView child_list;
    private BrandChildAdapter brandChildAdapter;
    private ISelectChild iSelectChild;
    private boolean isUpdate = true;
    private List<BrandChildAdapter> brandChildAdapterList;


    public BrandAdapter(Context context, List<BrandEntity> datas) {
        super(context, R.layout.item_brand, datas);
        brandChildAdapterList = new ArrayList<>();
    }

    @Override
    public void convert(ItemViewHolder holder, final BrandEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        brand_tv = holder.getItem().findViewById(R.id.brand_tv);
        child_list = holder.itemView.findViewById(R.id.child_list);
        brand_tv.setText(entity.getName());
        if (entity.getChildList() != null && entity.getChildList().size() > 0) {
            brand_tv.setVisibility(View.VISIBLE);
            child_list.setVisibility(View.VISIBLE);
        } else {
            brand_tv.setVisibility(View.GONE);
            child_list.setVisibility(View.GONE);
        }
        if (isUpdate) {
            brandChildAdapter = new BrandChildAdapter(context, null);
            brandChildAdapter.setItemSelect(true);
            child_list.setLayoutManager(new LinearLayoutManager(context));
            child_list.setAdapter(brandChildAdapter);
            brandChildAdapterList.add(brandChildAdapter);
        }
        brandChildAdapter.setOnItemClick(new AdapterOnItemClick<BrandEntity.BrandChild>() {
            @Override
            public void onItemClick(BrandEntity.BrandChild brandChild, int position) {
                iSelectChild.select(brandChild.getName(), brandChild.getId());
                int s = holder.getPosition();
                for (int i = 0; i < brandChildAdapterList.size(); i++) {
                    if (holder.getPosition() != i) {
                        brandChildAdapterList.get(i).resetCheckDatas();
                        brandChildAdapterList.get(i).setDefItem(-1);
                    }
                }
            }
        });
        brandChildAdapter.setDatas(datas.get(holder.getPosition()).getChildList());
    }

    public interface ISelectChild {
        void select(String name, String id);
    }

    public void iSelectChild(ISelectChild iSelectChild) {
        this.iSelectChild = iSelectChild;
    }

    public void IsUpdate(boolean isUpdate) {
        this.isUpdate = isUpdate;
        if (isUpdate) {
            if (brandChildAdapterList != null) {
                brandChildAdapterList.clear();
            }
        }
    }
}
